//
//  AJTTItemModel.swift
//  JourneyThroughTime
//
//  Created by iOS-Czz on 2023/11/6.
//

import UIKit

class AJTTItemViewModel{
    var dataSource = [AJTTItemModel]()
}
extension AJTTItemViewModel{
    func loadData(onSuccess: ((_ models: [AJTTItemModel]) ->Void)?){
        let dataHelper = AJTTItemModel.getUsingLKDBHelper()
        let sql = "select * from @t"
        let modelArr = dataHelper.search(withSQL: sql, to: AJTTItemModel.self) as! [AJTTItemModel]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy"
        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
            guard let date1 = dateFormatter.date(from: model1.invention),
                  let date2 = dateFormatter.date(from: model2.invention) else {
                return false // 如果转换失败则直接返回 false
            }
            return date1 < date2
        }
        self.dataSource = sortedModels
        onSuccess?(self.dataSource)
    }
    
}

@objcMembers
class AJTTItemModel: AJTTBaseModel,Codable {
    var name = ""
    var invention = ""
    var earlyPic = ""
    var earlyDesc = ""
    var modernPic = ""
    var modernDesc = ""
    var evolution = ""
    var isDefault = true
    override class func getTableName() -> String {
        return  String(describing: AJTTItemModel.self)
    }
}
